import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Test1 {
    //获取单词出现了多少次
    public static void main(String[] args) {
        String[] words={"Maybe","Maybe","hello","happy","sadness"};
        Map<String,Integer> map=countword(words);
        //使用map.entrySet()遍历map
        Set<Map.Entry<String,Integer>> entrySet=map.entrySet();
        //entrySet里面放的是Map.Entry<k,v>类型的
        for(Map.Entry<String,Integer> s:entrySet){
            System.out.println("key "+s.getKey()+" "+"val "+s.getValue());
        }
    }
    private static Map<String,Integer> countword(String[] words) {
        Map<String,Integer> map=new HashMap<>();
        //统计每个单词出现了多少次
        for(String s:words){
            if(map.get(s)==null){//则没有出现过一次
                map.put(s,1);
            }else{
                int val=map.get(s);
                map.put(s,val+1);
            }
        }
        return map;
    }
}